package com.easybenefit.UUService.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.easybeanfit.UUService.commutill.UUDialogUtill;
import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UULoadPictureUtill;
import com.easybeanfit.UUService.commutill.UUNetUtill;
import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybeanfit.UUService.config.EnumGridIndex;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybeanfit.UUService.config.UUDict;
import com.easybenefit.UUService.BaseActivity;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.adapter.MainGridViewAdapter;
import com.easybenefit.UUService.adapter.MainViewPagerAdapter;
import com.easybenefit.UUService.model.GridViewBean;
import com.easybenefit.UUService.model.HttpNewsBean;
import com.easybenefit.UUService.model.PlatNewsBean;
import com.easybenefit.UUService.model.UserInfo;
import com.easybenefit.UUService.myinterface.IActivityInterface;
import com.easybenefit.UUService.qrcode.QrcodeActivity;
import com.easybenefit.UUService.wight.MyViewPager;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements IActivityInterface, View.OnClickListener {
    private GridView gridView_main;//主界面gridview
    private MyViewPager viewPager_main;//主界面viewpager轮播控件
    private MainViewPagerAdapter mainViewPagerAdapter;//轮播adapter
    private MainGridViewAdapter mainGridViewAdapter;//gridadapter
    private List<PlatNewsBean> platNewsBeanList;//平台新闻数据list
    private List<GridViewBean> gridViewBeanList;//gird数据list
    private Context context;
    private ProgressDialog progressDialog;
    private TextView tv_topTitle;
    //Listener
    private Button btn_add;
    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initListener();
    }

    @Override
    public void initData() {
        getNewsList();
        context = this;
        platNewsBeanList = new ArrayList<PlatNewsBean>();
        userInfo = (UserInfo) UUSharedUtil.getObject(MainActivity.this, UUConfig.UU_USERINFO_NAME, UserInfo.class);
        mainViewPagerAdapter = new MainViewPagerAdapter(platNewsBeanList, context);
        viewPager_main.setAdapter(mainViewPagerAdapter);
        gridViewBeanList = new ArrayList<GridViewBean>();
        mainGridViewAdapter = new MainGridViewAdapter(gridViewBeanList, context);
        gridView_main.setAdapter(mainGridViewAdapter);
        GridViewBean gridViewBean1 = new GridViewBean();
        gridViewBean1.setName(EnumGridIndex.GLYHQ.getDes());
        gridViewBean1.setGridBackColorRes(R.color.top_background);
        gridViewBean1.setImageRes(getResources().getDrawable(R.drawable.manage));
        gridViewBean1.setCode(EnumGridIndex.GLYHQ.getCode());
        gridViewBean1.setNumber(0);
        GridViewBean gridViewBean2 = new GridViewBean();
        gridViewBean2.setName(EnumGridIndex.SHLM.getDes());
        gridViewBean2.setGridBackColorRes(R.color.bigblue);
        gridViewBean2.setImageRes(getResources().getDrawable(R.drawable.union));
        gridViewBean2.setCode(EnumGridIndex.SHLM.getCode());
        gridViewBean2.setNumber(userInfo.getMer_coop());
        GridViewBean gridViewBean3 = new GridViewBean();
        gridViewBean3.setName(EnumGridIndex.YHQPF.getDes());
        gridViewBean3.setGridBackColorRes(R.color.yellow);
        gridViewBean3.setImageRes(getResources().getDrawable(R.drawable.distributed));
        gridViewBean3.setCode(EnumGridIndex.YHQPF.getCode());
        gridViewBean3.setNumber(0);
        GridViewBean gridViewBean4 = new GridViewBean();
        gridViewBean4.setName(EnumGridIndex.YHQHX.getDes());
        gridViewBean4.setGridBackColorRes(R.color.smallblue);
        gridViewBean4.setImageRes(getResources().getDrawable(R.drawable.hexiao));
        gridViewBean4.setCode(EnumGridIndex.YHQHX.getCode());
        gridViewBean4.setNumber(0);

        tv_topTitle.setText(userInfo.getMer_name());
        if ("0".equals(userInfo.getIs_store())) {//0为商户，1为门店，
            gridViewBeanList.add(gridViewBean1);
            gridViewBeanList.add(gridViewBean2);
        }
        gridViewBeanList.add(gridViewBean3);
        gridViewBeanList.add(gridViewBean4);
        mainGridViewAdapter.notifyDataSetChanged();
    }

    @Override
    public void initView() {
        gridView_main = (GridView) findViewById(R.id.gv_main);
        viewPager_main = (MyViewPager) findViewById(R.id.main_view_pager);
        viewPager_main.startAutoScroll(1000);
        btn_add = (Button) findViewById(R.id.btn_add);
        tv_topTitle = (TextView) findViewById(R.id.top_title);
    }

    @Override
    public void initListener() {
        gridView_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv_code = (TextView) view.findViewById(R.id.tv_state_gridview_item);
                int code = Integer.valueOf(tv_code.getText().toString());
                Intent intent = null;
                if (code == EnumGridIndex.GLYHQ.getCode()) {
                    intent = new Intent(MainActivity.this, CouponsListActivity.class);
                } else if (code == EnumGridIndex.SHLM.getCode()) {
                    gridViewBeanList.get(position).setNumber(0);
                    mainGridViewAdapter.notifyDataSetChanged();
                    intent = new Intent(MainActivity.this, MerchantsListActivity.class);
                } else if (code == EnumGridIndex.YHQPF.getCode()) {
                    intent = new Intent(context, CouponsDistributedListActivity.class);
                } else if (code == EnumGridIndex.YHQHX.getCode()) {
                    intent = new Intent(context, QrcodeActivity.class);
                    intent.putExtra("type", MainActivity.class.getSimpleName());
                }
                startActivity(intent);
            }
        });
        btn_add.setOnClickListener(this);
    }

    /**
     * 获取轮播的view
     *
     * @return
     */
    public View getPlatNewView(String url) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_viewpager, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.image_item_viewpager);
        UULoadPictureUtill load = new UULoadPictureUtill(context, imageView);
        load.getmImageLoader().get(url, load.getOne_listener());
        return view;
    }

    /**
     * 获取新闻列表数据
     */
    public void getNewsList() {
        if (!UUNetUtill.isConnected(MainActivity.this)) {
            UUToastUtil.showToast(MainActivity.this, UUDict.NOT_INTERNET);
        } else {
            AsyncHttpClient client = new AsyncHttpClient();
            /** 获取新闻列表 */
            String url = UUConfig.SERVER_BASE_URL + "platnews";
            client.post(url, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {
                    progressDialog = UUDialogUtill.getLoadingBar(MainActivity.this, "加载中....");
                    progressDialog.show();
                }

                @Override
                public void onFinish() {
                    progressDialog.dismiss();
                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String content = new String(bytes);
                    HttpNewsBean responseBean = (HttpNewsBean) UUJsonUtil.fromJson(content, HttpNewsBean.class);
                    for (PlatNewsBean bean : responseBean.getData().getNewslist()) {
                        bean.setView(getPlatNewView(UUConfig.BASE_URL + bean.getNews_img()));
                        platNewsBeanList.add(bean);
                    }
                    mainViewPagerAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add:
                Intent intent = new Intent(context, SettingActivity.class);
                startActivity(intent);
                break;

        }
    }
}
